/**
 * Copyright (c) 2010 icer He <icerhe@gmail.com>
 */
package com.icerhe.trigger;


import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

/**
 * @author icer
 * 
 */
public class TaskWakeLock {

    private static PowerManager.WakeLock sCpuWakeLock;

    public static synchronized void acquireWakeLock(Context context) {
        Log.v(Consts.TAG, "Acquiring cpu wake lock");
        if (sCpuWakeLock != null) {
            return;
        }

        PowerManager pm =
                (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        sCpuWakeLock = pm.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, Consts.TAG);
        sCpuWakeLock.acquire();
    }

    public static synchronized void releaseLock() {
        Log.v(Consts.TAG, "Releasing cpu wake lock");
        if (sCpuWakeLock != null) {
            sCpuWakeLock.release();
            sCpuWakeLock = null;
        }
    }
}
